<?php

namespace Gds\ArticleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gds\ArticleBundle\Entity\Departement;
use Gds\ArticleBundle\Form\DepartementType;
use Symfony\Component\HttpFoundation\Response;
use Gds\ArticleBundle\Entity\Stock;

class DepartementController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $dql = "SELECT c FROM GdsArticleBundle:Departement c WHERE (c.is_supprimer=:is_supprimer and c.nom LIKE :nom and c.type LIKE :type and c.ville LIKE :ville)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'nom' => '%' . $request->get('nom') . '%', 'type' => '%' . $request->get('type_dep') . '%', 'ville' => '%' . $request->get('ville') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsArticleBundle:Departement:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsArticleBundle:Departement:index.html.twig', array('pagination' => $pagination));
    }

    public function newAction() {
        $departement = new Departement();
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $departement->setIsSupprimer(false);
        $form = $this->createForm(new DepartementType(), $departement);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $departement = $form->getData();
                $em->persist($departement);
                $articles = $em->getRepository('GdsArticleBundle:Article')->findAll();
                foreach ($articles as $article) {
                    $stock = new Stock();
                    $stock->setArticle($article);
                    $stock->setQuantite(0);
                    $stock->setQuantiteMin(0);
                    $stock->setQuantiteNoir(0);
                    $stock->setDepartement($departement);
                    $em->persist($stock);
                }
                $em->flush();
                $red = $request->get('red');
                if ($red == 'n') {
                    $departement = new Departement();
                    $departement->setIsSupprimer(false);
                    $form = $this->createForm(new DepartementType(), $departement);
                    return $this->render('GdsArticleBundle:Departement:new.html.twig', array('form' => $form->createView()));
                } else {
                    $em = $this->getDoctrine()->getEntityManager();
                    $request = $this->getRequest();
                    $dql = "SELECT c FROM GdsArticleBundle:Departement c WHERE (c.is_supprimer=:is_supprimer and c.nom LIKE :nom and c.type LIKE :type and c.ville LIKE :ville)";
                    $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'nom' => '%' . $request->get('nom') . '%', 'type' => '%' . $request->get('type_dep') . '%', 'ville' => '%' . $request->get('ville') . '%'));
                    $paginator = $this->get('knp_paginator');
                    $pagination = $paginator->paginate(
                            $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                    );
                    $pagination->setUsedRoute('gds_departement_list');
                    if ($request->get('type')) {
                        return $this->render('GdsArticleBundle:Departement:pageList.html.twig', array('pagination' => $pagination));
                    }
                    return $this->render('GdsArticleBundle:Departement:list.html.twig', array('pagination' => $pagination));
                }
            }
        }
        return $this->render('GdsArticleBundle:Departement:new.html.twig', array('form' => $form->createView()));
    }

    public function listAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $dql = "SELECT c FROM GdsArticleBundle:Departement c WHERE (c.is_supprimer=:is_supprimer and c.nom LIKE :nom and c.type LIKE :type and c.ville LIKE :ville)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'nom' => '%' . $request->get('nom') . '%', 'type' => '%' . $request->get('type_dep') . '%', 'ville' => '%' . $request->get('ville') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        if ($request->get('type')) {
            return $this->render('GdsArticleBundle:Departement:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsArticleBundle:Departement:list.html.twig', array('pagination' => $pagination));
    }

    public function deleteAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $departement = $em->getRepository('GdsArticleBundle:Departement')->find($id);
        $departement->setIsSupprimer(true);
        $em->persist($departement);
        $em->flush();
        $dql = "SELECT c FROM GdsArticleBundle:Departement c WHERE (c.is_supprimer=:is_supprimer and c.nom LIKE :nom and c.type LIKE :type and c.ville LIKE :ville)";
        $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'nom' => '%' . $request->get('nom') . '%', 'type' => '%' . $request->get('type_dep') . '%', 'ville' => '%' . $request->get('ville') . '%'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $query, $request->get('page', 1)/* page number */, 10/* limit per page */
        );
        $pagination->setUsedRoute('gds_departement_list');
        if ($request->get('type')) {
            return $this->render('GdsArticleBundle:Departement:pageList.html.twig', array('pagination' => $pagination));
        }
        return $this->render('GdsArticleBundle:Departement:list.html.twig', array('pagination' => $pagination));
    }

    public function deleteConfAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $departement = $em->getRepository('GdsArticleBundle:Departement')->find($id);
        return $this->render('GdsArticleBundle:Departement:deleteConf.html.twig', array('departement' => $departement));
    }

    public function editAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        $departement = $em->getRepository('GdsArticleBundle:Departement')->find($id);
        $form = $this->createForm(new DepartementType(), $departement);
        if ($request->isMethod('post')) {
            $form->bind($request);
            if ($form->isValid()) {
                $departement = $form->getData();
                $em->persist($departement);
                $em->flush();
                $em = $this->getDoctrine()->getEntityManager();
                $request = $this->getRequest();
                $dql = "SELECT c FROM GdsArticleBundle:Departement c WHERE (c.is_supprimer=:is_supprimer and c.nom LIKE :nom and c.type LIKE :type and c.ville LIKE :ville)";
                $query = $em->createQuery($dql)->setParameters(array('is_supprimer' => false, 'nom' => '%' . $request->get('nom') . '%', 'type' => '%' . $request->get('type_dep') . '%', 'ville' => '%' . $request->get('ville') . '%'));
                $paginator = $this->get('knp_paginator');
                $pagination = $paginator->paginate(
                        $query, $request->get('page', 1)/* page number */, 10/* limit per page */
                );
                $pagination->setUsedRoute('gds_departement_list');
                if ($request->get('type')) {
                    return $this->render('GdsArticleBundle:Departement:pageList.html.twig', array('pagination' => $pagination));
                }
                return $this->render('GdsArticleBundle:Departement:list.html.twig', array('pagination' => $pagination));
            }
        }
        return $this->render('GdsArticleBundle:Departement:edit.html.twig', array('id' => $departement->getId(), 'form' => $form->createView()));
    }

    public function listForArticleAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $departements = $em->getRepository('GdsArticleBundle:Departement')->findAll();
        return $this->render('GdsArticleBundle:Departement:listForArticle.html.twig', array('departements' => $departements));
    }

    public function testNomAction() {
        $request = $this->getRequest();
        $nom = $request->get('nom');
        $id = $request->get('id');
        if ($id) {
            $departement = $this->getDoctrine()->getEntityManager()->getRepository('GdsArticleBundle:Departement')->getByNotId($id, $nom);
        } else {
            $departement = $this->getDoctrine()->getEntityManager()->getRepository('GdsArticleBundle:Departement')->findBy(array('nom' => $nom));
        }
        if (count($departement) != 0) {
            return new Response(json_encode(array('success' => 0)));
        } else {
            return new Response(json_encode(array('success' => 1)));
        }
    }

    public function listForStockAction() {
        $departements=  $this->getDoctrine()->getEntityManager()->getRepository('GdsArticleBundle:Departement')->findBy(array('is_supprimer'=>false));
        return $this->render('GdsArticleBundle:Departement:listForStock.html.twig', array('departements' => $departements));
    }
}
